<script setup lang="ts">
  import {reactive, ref} from "vue";
  import store from 'store'
  import {userModify, userModifyPassword} from '@/api/system/user.ts'
  import { ElMessage } from 'element-plus'
  let user:any = ref({})
  user.value = store.get('user')

  let form = reactive(Object.assign({}, user.value))

  let passwdForm = reactive({
    oldPassword:'',
    newPassword:'',
    confirmPassword:''
  })
  let activeName = ref('first')


  //保存用户信息
  const save = async () =>{
    await userModify(form)
    store.set('user',form)
    user.value = form
    ElMessage({
      message: '成功修改用户基础信息',
      type: 'success',
    })
  }
  //修改密码
  const modifyPassword = async () =>{
    if(passwdForm.newPassword!== passwdForm.confirmPassword){
      ElMessage({
        message: '两次输入的密码不一致',
        type: 'error',
      })
      return
    }
    await userModifyPassword(passwdForm.oldPassword, passwdForm.newPassword)
    ElMessage({
      message: '成功修改用户密码',
      type:'success',
    })
  }
  //重置密码
  const cleanPassword = () =>{
    passwdForm.oldPassword = ''
    passwdForm.newPassword = ''
    passwdForm.confirmPassword = ''
  }

  function handleClick(){

  }
</script>

<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card">
          <div>个人信息</div>
          <el-divider />

          <el-image style="margin:0 0 10px 0;width: 100px; height: 100px; text-align: center; border-radius: 100px" :src="'http://180.76.175.49:10000/prod-api/'+user.avatar" fit="fill" />

          <ul>
            <li>
              <span>用户名称：</span>
              <span>{{user.nickName}}</span>
            </li>
            <li>
              <span>手机号码：</span>
              <span>{{user.phonenumber}}</span>
            </li>
            <li>
              <span>用户邮箱：</span>
              <span>{{user.email}}</span>
            </li>
            <li>
              <span>所属部门：</span>
              <span>{{user.dept.deptName}}</span>
            </li>
            <li>
              <span>所属角色：</span>
              <span>{{user.roles[0].roleName}}</span>
            </li>
            <li>
              <span>创建日期：</span>
              <span>{{user.createTime}}</span>
            </li>
            <li>
              <span>最后登陆时间：</span>
              <span>{{user.loginDate}}</span>
            </li>
            <li>
              <span>最后登陆IP：</span>
              <span>{{user.loginIp}}</span>
            </li>
          </ul>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card class="box-card">
          <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
            <el-tab-pane label="基本资料" name="first">
              <div style="height: 20px; width: 100%"></div>
              <el-form :model="form" label-width="120px">
                <el-form-item label="称呼">
                  <el-input v-model="form.nickName" />
                </el-form-item>
                <el-form-item label="手机">
                  <el-input v-model="form.phonenumber" />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="form.email" />
                </el-form-item>
                <el-form-item label="性别">
                  <el-radio-group v-model="form.sex">
                    <el-radio label="1" >男</el-radio>
                    <el-radio label="0">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="save">保存</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="second">
              <div style="height: 20px; width: 100%"></div>
              <el-form :model="passwdForm" label-width="120px">
                <el-form-item label="原始密码">
                  <el-input v-model="passwdForm.oldPassword" />
                </el-form-item>
                <el-form-item label="修改密码">
                  <el-input v-model="passwdForm.newPassword" />
                </el-form-item>
                <el-form-item label="再次输入">
                  <el-input v-model="passwdForm.confirmPassword" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="modifyPassword">修改密码</el-button>
                  <el-button type="default" @click="cleanPassword">重置填写</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
  li{
    padding:5px 10px;
    border-radius: 10px;
    line-height: 25px;
    cursor: pointer;
    span{
      padding-right: 5px;
    }
  }
  li:hover{
    background-color: rgba(221, 221, 221, 0.99);
  }
</style>